#ifndef FACEDETECTER_H
#define FACEDETECTER_H


#include <opencv/cv.h>
#include <opencv/cvwimage.h>
#include <opencv/cvaux.h>
#include <opencv/cxcore.h>
#include <opencv/cxmisc.h>
#include <opencv/highgui.h>
#include <opencv/ml.h>


#define NO_FIST_FOUND 0
#define SINGLE_FIST_FOUND 1
#define MANY_FISTS_FOUND 2



////////////////////////////////////////////

class ItemDetecter
{
 private:
  /// classifier that recognizes face
  cv::CascadeClassifier fist_cascade;
  /// classifier that recognizes palm
  cv::CascadeClassifier palm_cascade;
  /* ROI of the image */
  cv::Rect ROI;
  /* the position of the roi */
  cv::Point center;
  /* whether we are tracking (using the roi) or not */
  bool isTracking;
 public:
  /* constructor */
  /* load the HAAR files */
  inline ItemDetecter () 
  {
    /* paths to description of what we're looking for */
    std::string fist_cascade_name = "xml/fist.xml";
    std::string palm_cascade_name = "xml/palm.xml";
    //-- 1. Load the cascades
    if( !fist_cascade.load( fist_cascade_name ) ){ printf("FistDetecter:--(!)Error loading %s\n",fist_cascade_name.c_str()); exit(-1); };
    if( !palm_cascade.load( palm_cascade_name ) ){ printf("PalmDetecter:--(!)Error loading %s\n",palm_cascade_name.c_str()); exit(-1); };
    cv::RNG rng(12345);

    isTracking=false;
  }
  /* look for the item in the image */
  /* relies on previous ROI if we are tracking */
  /* set ROI and pos if found */
  /* otherwise we've lost the item and we're not tracking  */
  int detectAndReframe(cv::Mat img);
  /* compute position of the ROI */
  void computeROIPosition();
  /* return a grown ROI, the item might have moved a little bit */
  cv::Rect growROI (int factorGrow=100) const;
  /** 
   * This function is called to take a picture from the user's webcam.
   * 
   * 
   * @return the image taken
   */
  cv::Mat takePictureFromWebcam();
  /* reframe a rect within an image (check for boundaries clash) */
  void reframeBoundaries(cv::Mat imgIn,cv::Rect& roi) const;
  void drawROI(cv::Mat imgIn) const;
  cv::Mat extractROI(cv::Mat img) const;
  inline cv::Point getCenter() const { return center; }

};


#endif
